package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
public class SkuManageController {

    @Autowired
    private ManageService manageService;

    //保存sku
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        this.manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //根据三级分类id查询skuInfo列表
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,SkuInfo skuInfo){

        Page<SkuInfo> skuInfoPage = new Page<>(page,limit);
        IPage<SkuInfo> infoIPage = this.manageService.getSkuInfoList(skuInfoPage,skuInfo);
        return Result.ok(infoIPage);
    }
    //上架
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        this.manageService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        this.manageService.cancelSale(skuId);
        return Result.ok();
    }
    @GetMapping("getSkuInfo/{skuId}")
    public Result getSkuInfo(@PathVariable Long skuId){
        SkuInfo skuInfo = this.manageService.getSkuInfo(skuId);
        return Result.ok(skuInfo);
    }
    //修改skuInfo
    @PostMapping("updateSkuInfo")
    public Result updateSpuInfo(@RequestBody SkuInfo skuInfo){
        this.manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
}
